package com.group.orderproject.Controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileController {

    @Value("${file.upload.path}")
    private String fileUploadPath;
    @PostMapping("/upload")
    public String upload(@RequestParam MultipartFile file) throws IOException {
        String originalFilename=file.getOriginalFilename();
        String type= originalFilename.substring(originalFilename.lastIndexOf("."));
        //存储到磁盘
        File uploadParentFile=new File(fileUploadPath);
        if(!uploadParentFile.exists()){
            uploadParentFile.mkdir();
        }
        //定义一个文件唯一识别码
        UUID uuid = UUID.randomUUID();
        String uid=uuid.toString().replace("-", "").toUpperCase();
        File uploadFile=new File(fileUploadPath+uid+type);
        file.transferTo(uploadFile);
        return "http://localhost:8081/image/"+uid+type;
    }

}